package com.itheima.Service.Servicelmpl;

import com.itheima.Service.MemberService;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.exception.OrderException;
import com.itheima.mapper.MemberMapper;
import com.itheima.pojo.Login;
import com.itheima.pojo.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

@Service
public class MemberServicelmpl implements MemberService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MemberMapper memberMapper;
    /**
     * 用户登录
     * @param login
     */
    public void login(Login login) {
        //校验验证码
        String key="code";
        if (!login.getValidateCode().equals(redisTemplate.opsForValue().get(key))){
            throw new OrderException(MessageConstant.VALIDATECODE_ERROR);
        }
        //判断是不是会员
        String telephone = login.getTelephone();
        Member phonelist = memberMapper.phonelist(telephone);
        if (phonelist==null){
            //自动注册会员
            login.setRegTime(LocalDate.now());
            memberMapper.INSERT(login.getTelephone(),login.getRegTime());
        }

    }
}
